<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sell extends CI_Controller {
    function Sell() 
    {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('file');
        $this->load->helper('captcha');
        $this->load->helper(array('form', 'url'));
    }
    
    public function index($msg = NULL)
    {
        $data['message'] = $msg;
        if (!$this->session->userdata('validated') ||
           ($this->session->userdata('membership') == 0))
        {            
            $data['service'] = $this->db->get('psacar_service');
            $data['location'] = $this->db->get('psacar_location');

            $this->load->view('header', $data);
            $this->load->view('sell', $data);

            $data['make'] = $this->db->get('psacar_make');
            $this->load->view('footer', $data);
        }
        else
            redirect(base_url(). 'index.php/sell/result');
    }
    
    public function detail()
    {
        $data['message'] = "";

        //Variable
        $car_id = $this->uri->segment(3);        
        
        //Query: Car list
        $this->load->model('Car_model');
        $data['car_list'] = $this->Car_model->get_car_by_id($car_id);

        //Query: Comment
        $this->load->model('Comment_model');
        $data['comment_list'] = $this->Comment_model->get_comments_by_car($car_id);	

        $this->load->view('header', $data);
        $this->load->view('templates/car/car_detail', $data);
        
        $data['make']= $this->db->get('psacar_make');
        $this->load->view('footer', $data);
    }
    
    public function result()
    {
        $email = $this->security->xss_clean($this->input->post('email'));
        $password = $this->security->xss_clean($this->input->post('password'));

        if (($email) && ($password))
        {
            $this->load->model('Member_model');
            $this->Member_model->login($email, $password);
        }

        if ($this->session->userdata('validated'))
        {
            $page_view = "sell";
            if($this->session->userdata('membership') != 4)
            {                       
                //Car
                $this->load->model('Car_model');
                $data['car_list'] = $this->Car_model->get_approved_car_by_member_id($this->session->userdata('userid'));
                $data['number_car_approved'] = $data['car_list']->num_rows;
                $data['car_list'] = $this->Car_model->get_pending_car_by_member_id($this->session->userdata('userid'));
                $data['number_car_pending'] = $data['car_list']->num_rows;
                $data['car_list'] = $this->Car_model->get_expired_car_by_member_id($this->session->userdata('userid'));
                $data['number_car_expired'] = $data['car_list']->num_rows;                    
                
                //Service
                $data['number_service_approved'] = 
                    $this->db->get_where(
                        'psacar_member_service', 
                        array(
                            'member_id'=>$this->session->userdata('userid'), 
                            'status'=>1))->num_rows; 
                $data['number_service_pending'] = 
                    $this->db->get_where(
                        'psacar_member_service', 
                        array(
                            'member_id'=>$this->session->userdata('userid'), 
                            'status'=>0))->num_rows; 
                $data['number_service_expired'] = 0;
                
                //Product
                $data['number_part_approved'] = 
                    $this->db->get_where(
                        'psacar_member_part', 
                        array(
                            'member_id'=>$this->session->userdata('userid'), 
                            'status'=>1))->num_rows; 
                $data['number_part_pending'] = 
                    $this->db->get_where(
                        'psacar_member_part', 
                        array(
                            'member_id'=>$this->session->userdata('userid'), 
                            'status'=>0))->num_rows; 
                $data['number_part_expired'] = 0;
                
                $page_view = 'templates/member/member_car_list';
            }

            $data['message'] = "";
            $this->load->view('header', $data);
            $this->load->view($page_view, $data);
            $data['make']= $this->db->get('psacar_make');
            $this->load->view('footer', $data);              
        }
        else
        {
            $data['message'] = "<font color=red>Invalid user name or password</font>";

            $data['service'] = $this->db->get('psacar_service');
            $data['location'] = $this->db->get('psacar_location');

            $this->load->view('header', $data);
            $this->load->view('sell', $data);

            $data['make'] = $this->db->get('psacar_make');
            $this->load->view('footer', $data);            
        }   
    }	  
        
    public function car()
    {       
        $data['message'] = "";
        if (!$this->session->userdata('validated') ||
           ($this->session->userdata('membership') == 0))
        {            
            $data['service'] = $this->db->get('psacar_service');
            $data['location'] = $this->db->get('psacar_location');

            $this->load->view('header', $data);
            $this->load->view('sell', $data);

            $data['make'] = $this->db->get('psacar_make');
            $this->load->view('footer', $data);
        }
        else
            redirect(base_url(). 'index.php/sell/result');
    }    
}